package com.killer_jin.core.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * swagger 插件自定义配置文件
 * @ Configuration可理解为用spring的时候xml里面的<beans>标签。
 * @ Bean可理解为用spring的时候xml里面的<bean>标签。
 * ref: https://springfox.github.io/springfox/docs/current/#introduction
 * @author jiayu 2019-07-10
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api1() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("Authorization").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();

        pars.add(tokenPar.build());

        // [ Base URL: tjjh.9daye.cn/ ] https://tjjh.9daye.cn/v2/api-docs?group=门店车主API
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("项目名API,如: 门店车主API")// Todo
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.czh.ecom.api"))// Todo
                .paths(PathSelectors.ant("/api/v?/**"))// Todo
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                // .globalOperationParameters(pars)
                .apiInfo(apiInfo());

    }

    private List<ApiKey> securitySchemes() {
        return newArrayList(
                new ApiKey("Authorization", "Authorization", "header"));
    }

    private List<SecurityContext> securityContexts() {
        return newArrayList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build()
        );
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(
                new SecurityReference("Authorization", authorizationScopes));
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(// Todo
                "9daye Ecom Data API",
                "API文档，有问题请联系我们 - 创智汇技术团队\n " +
                        "需要认证的API，不提供Authorization头、提供错误、无效、过期的返回401",
                "API TOS",
                "Terms of service",
                new Contact("创智汇研发中心", "", ""),
                "License of API", "API license URL", Collections.emptyList());
    }
}
